package com.lz.gmall.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.lz.gmall.cart.entity.SkuIdAndPrice;
import com.lz.gmall.constant.GmallConstant;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@CanalEventListener  //声明这是一个canalClient的监听器
@Slf4j
public class CanalClientListener {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * ListenPoint属性
     * schema数据库
     * table数据表（可以多个）
     * eventType操作类型，不指定表示所有操作
     */
//    @ListenPoint(schema = "gmall_product",table = {"sku_info"})
//    public void skuInfoUpdateRedisCache(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
//        //获取修改之前的数据库信息
//        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
//        beforeColumnsList.stream().forEach(column -> {
//            String columnName = column.getName();
//            String columnValue = column.getValue();
//            System.out.println(columnName+":"+columnValue);
//        });
//        System.out.println("---------------------------------------------------");
//        //获取修改之后的数据库信息
//        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
//        afterColumnsList.stream().forEach(column -> {
//            String columnName = column.getName();
//            String columnValue = column.getValue();
//            //拿到skuId，对redisCache进行修改
//            if ("id".equalsIgnoreCase(columnName)){
////                redisTemplate.delete(GmallConstant.REDIS_SKU_INFO+columnValue);
////                log.info("redis数据删除了，skuId"+columnValue);
//                System.out.println(columnName+columnValue);
//            }
//        });
//    }

    @ListenPoint(schema = "gmall_product",table = {"sku_info"})
    public void skuInfoUpdateRedisCache(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //获取修改之后的数据库信息
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        SkuIdAndPrice skuIdAndPrice = new SkuIdAndPrice();
        afterColumnsList.stream().forEach(column -> {
            String columnName = column.getName();
            String columnValue = column.getValue();
            //拿到skuId，对redisCache进行修改
            if ("id".equalsIgnoreCase(columnName)){
                skuIdAndPrice.setSkuId(Long.parseLong(columnValue));
                System.out.println(columnName+columnValue);
            }
            if ("price".equalsIgnoreCase(columnName)){
                skuIdAndPrice.setPrice(new BigDecimal(columnValue));
                System.out.println(columnName+columnValue);
            }
        });

        redisTemplate.opsForHash().put("sku:info:price",String.valueOf(skuIdAndPrice.getSkuId()),String.valueOf(skuIdAndPrice.getPrice()));
    }

}
